package com.farsunset.ichat.cim.handler;

import org.apache.log4j.Logger;

import com.farsunset.cim.nio.constant.CIMConstant;
import com.farsunset.cim.nio.handler.CIMRequestHandler;
import com.farsunset.cim.nio.mutual.ReplyBody;
import com.farsunset.cim.nio.mutual.SentBody;
import com.farsunset.cim.nio.session.CIMSession;
import com.farsunset.ichat.common.util.ContextHolder;
import com.kuaigou.cim.model.TrackBo;
import com.kuaigou.cim.service.TruckerNewPositionSendService;
import com.kuaigou.cim.util.JSONHelper;
import com.kuaigou.common.model.bean.Position;

/**
 * 司机上报最新位置点
 *
 */
public class TruckerPositionReportHandler implements CIMRequestHandler {
	
	protected final Logger logger = Logger.getLogger(TruckerPositionReportHandler.class);
	
	public ReplyBody process(CIMSession newSession, SentBody message) {	
		TruckerNewPositionSendService truckerNewPositionSendService = (TruckerNewPositionSendService)ContextHolder.getBean("truckerNewPositionSendService");
		ReplyBody reply = new ReplyBody();
		try {
			reply.setKey(CIMConstant.RequestKey.CLIENT_TRUCKER_POSITION_REPORT);
			String position = message.get("position");
			logger.error("position=" + position);
			TrackBo trackbo = JSONHelper.jsonToEntity(position, TrackBo.class);
			Position positions = convertTo(trackbo);
			truckerNewPositionSendService.send(positions);
			reply.setCode(CIMConstant.ReturnCode.CODE_200);
		} catch (Exception e) {
			reply.setCode(CIMConstant.ReturnCode.CODE_500);
			e.printStackTrace();
		}
		return reply;
	}	
	
	private Position convertTo(TrackBo trackbo){
			Position position = new Position(); 
			position.setAccountId(trackbo.getAccount());
			position.setCityCode(trackbo.getCityCode());
			position.setCityId(trackbo.getCityId());
			position.setDirection(trackbo.getDirection());
			position.setAltitude(trackbo.getAltitude());
			position.setGpsTime(trackbo.getGpsTime());
			position.setAddress(trackbo.getAddress());
			position.setLatitude(trackbo.getLatitude());
			position.setLongitude(trackbo.getLongitude());
			position.setSpeed(trackbo.getSpeed());
			position.setAccessTime(System.currentTimeMillis());
	        return position;
	}

}
